﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Windows.Forms;

namespace ELS.User
{
    public class PermissionController
    {
        PermissionDataAccess permissionDataAccess = new PermissionDataAccess();       

        internal DataTable GetAllPermission()
        {            
            DataTable dtAllGroups = new DataTable();
            dtAllGroups = permissionDataAccess.SelectAllPermission();
            return dtAllGroups;            
        }

        internal Dictionary<String, int> GetAllPermissionByDictionary()
        {
            Dictionary<String, int> Groups = new Dictionary<String, int>();
            DataTable dtAllGroups = new DataTable();
            dtAllGroups = permissionDataAccess.SelectAllPermission();
            for (int i = 0; i < dtAllGroups.Rows.Count; i++)
            {
                Groups.Add(dtAllGroups.Rows[i]["CODE"].ToString(), int.Parse(dtAllGroups.Rows[i]["ID"].ToString()));
            }
            return Groups;
        }

        internal Dictionary<string, int> GetPermissionByGroupId(int groupId)
        {
            Dictionary<String, int> permissionOfGroup = new Dictionary<String, int>();
            try
            {
                DataTable dtPermission = new DataTable();
                dtPermission = permissionDataAccess.SelectPermissionByGroupId(groupId);
                if (dtPermission.Rows.Count > 0)
                {
                    for (int i = 0; i < dtPermission.Rows.Count; i++)
                    {
                        permissionOfGroup.Add(dtPermission.Rows[i]["PER_CD"].ToString(), int.Parse(dtPermission.Rows[i]["PER_ID"].ToString()));
                    }
                }
            }
            catch (Exception e)
            {
                return permissionOfGroup;
            }
            return permissionOfGroup;
        }

        internal DataTable SelectPermissionByCode(string permissionCode)
        {
            return permissionDataAccess.SelectPermissionByCode(permissionCode);
        }

        internal bool UpdatePermission(int currentPermissionCode, string perName, string perDes)
        {
            //int permissionId = int.Parse(permissionDataAccess.SelectPermissionByCode(currentPermissionCode).Rows[0]["PER_ID"].ToString());
            int rowUpdate = permissionDataAccess.UpdatePermission(currentPermissionCode, perName, perDes);

            if (rowUpdate > 0)
                return true;
            else
                return false;
        }

        internal bool AddNewPermission(string perCode, string perDes)
        {
            bool isExistPermission = permissionDataAccess.SelectPermissionByCode(perCode).Rows.Count > 0;
            int rowInsert = permissionDataAccess.InsertPermission(perCode, perDes);

            if (rowInsert > 0)
                return true;
            else
                return false;
        }
        GrantDataAccess grantDataAccess = new GrantDataAccess();
        
        internal bool DeleteSelectPermission(List<int> list)
        {
            string listPermission = "";
            for (int i = 0; i < list.Count; i++)
            {
                listPermission += list[i].ToString() + ",";
            }
            listPermission = listPermission.Substring(0, listPermission.Length - 1);

            bool isDeleteGrpPerSuccess = grantDataAccess.DeleteUsingGrpPerInListByPermissionId(listPermission) >= 0;

            if (isDeleteGrpPerSuccess)
            {
                bool isDeleteSuccess = permissionDataAccess.DeletePermission(listPermission) > 0;
                if (isDeleteSuccess)
                    return true;
                else
                    return false;
            }
            else return false;
        }

        internal DataTable SelectPermissionById(int perId)
        {
            return permissionDataAccess.SelectPermissionById(perId);
        }
    }
}
